#!/bin/bash
languages=$(ls languages | grep -v 'template')

cat index.rst > index_.rst

echo 'Covered languages' >> index_.rst
echo '=================' >> index_.rst

for l in $languages
do
    cd languages/$l
    main=$(cat index.yml | yq .main)
    name=$(cat index.yml | yq .name.eng | sed 's/\"//g')
    translations=$(ls | grep rst | sed 's/.rst//')  # yq keys
    links=''
    for t in $translations
    do
        links=$(echo "$links \`$t <$t.html>\`_")
    done
    links=$(echo "$links" | sed 's/ //')
    for t in $translations
    do
        name_t=$(cat index.yml | yq ".name.$t" | sed 's/\"//g')
        echo "$links" > ${t}_.rst
        echo "" >> ${t}_.rst
        cat $t.rst >> ${t}_.rst
        pandoc --css ../../main.css --css ../../pandoc.css --metadata pagetitle="$name_t" -t html5 --self-contained ${t}_.rst > $t.html
        rm ${t}_.rst
    done
    cd ../..
    echo "- \`$name <languages/$l/eng.html>\`_" >> index_.rst
done

pandoc --css main.css --css pandoc.css --metadata pagetitle="Kaurselyle’u" -t html5 --self-contained index_.rst > index.html
rm index_.rst
